package net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import operator.Operation;
import util.Protocol;

public class ClientThread extends Thread
{
    Socket tcpConnection;
    
    byte[] buffer = new byte[256];
    
    byte[] bufferString = new byte[256];
    
    Operation operation = new Operation();          
    
    InputStream in;
    
    OutputStream out;
    
    Protocol protocol = new Protocol();
    
    public ClientThread(Socket tcpConnection)
    {
        this.tcpConnection = tcpConnection;
    }

    public void run()
    {
        try {
            in = tcpConnection.getInputStream();
            out = tcpConnection.getOutputStream();
            
            while (true)
            {
                int count = in.read(buffer);
                String receiveString = new String(buffer,0,count);
                System.out.println(receiveString);
                //对收到的内容进行解析
                String[] massagecontent = receiveString.split(":");
                String head =  massagecontent[0];
                String massage = massagecontent[1];
                
                if (head.equals("LogIn"))
                {
                    String[] content = massage.split(",");  
                    String username = content[0];
                    String password = content[1];
                    
                    if(operation.checkloginaccount(username, password))
                    {
                        String account = username;
                        //登陆成功后将用户线程放入HashMap中保存
                        Server.clientPool.put(account, this);
                        String  friendID =operation.returnfriedlist(username);
                        String finalmassage = "success;" +friendID ; 
                        this.sendtoClient(finalmassage);                                        
                    }
                    else{
                        sendtoClient("fail");
                    }
                }
                
                if (head.equals("SendMassage"))
                {
                    String[] content = massage.split(",");  
                    String from = content[0];
                    String to = content[1];
                    String conversation = content[2];
                    String[] afrom = from.split(";");
                    String postername = afrom[1];
                    String[] ato = to.split(";");
                    String receiver = ato[1];
                    String postmassage = protocol.postFriendmassage(postername, conversation,receiver);
                            System.out.println(postmassage);
                    ClientThread toClient = Server.clientPool.get(receiver);
                    //从池中将相应的线程取出，并将消息发送至目标用户
                    //若目标用户离线，不作处理，用户可自行添加离线消息功能
                    if(toClient!=null){
                        toClient.sendtoClient(postmassage);
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void sendtoClient(String message)
    {
        try 
        {
            out.write(message.getBytes());
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
